home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-05
/
s13237.zip
/
UPSTATSR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-30
|
4KB
|
147 lines
/* *******************************************************************/
/* */
/* SQL Server 'Update Statistics' utility (DOS) */
/* */
/* Dynamicly updates statistics on all tables within the specified */
/* database. */
/* */
/* USAGE: upstatsr server loginid database [password] */
/* */
/* By Damien Lindauer */
/* Microsoft Corp. */
/* */
/* *******************************************************************/
#define DBMSDOS
#include <stdio.h>
#include <sqlfront.h>
#include <sqldb.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define STDEXIT 0
int err_handler();
int msg_handler();
main(int argc, char *argv[])
{
LOGINREC *loginS, *loginU;
DBPROCESS *dbprocS, *dbprocU;
DBCHAR Name[30];
char Server[30], LoginID[30], Database[30], Password[30];
char SelectBuf[50];
int Count;
dberrhandle(err_handler);
dbmsghandle(msg_handler);
if ((argc < 4) || (argc > 5))
{
printf("\nERROR: Invalid number of arguments.");
printf("\nUSAGE: upstatsr server loginid database [password]\n");
exit(STDEXIT);
};
strcpy(Server, argv[1]);
strcpy(LoginID, argv[2]);
strcpy(Database, argv[3]);
if (argc == 4)
strcpy(Password, "");
else
strcpy(Password, argv[4]);
printf("\nSQL Server Update Statistics Utility\n");
printf("--------------------------------------------------------------\n");
printf("Last Modified: 03/04/91 By: Damien Linaduer\n");
printf("--------------------------------------------------------------\n");
printf("(Updates statistics for all tables in the specified database.)\n\n");
printf("Attempting to connect to %s...", Server);
loginS = dblogin();
DBSETLUSER(loginS, LoginID);
DBSETLPWD(loginS, Password);
DBSETLAPP(loginS, "UpStats_S");
dbprocS = dbopen(loginS, Server);
loginU = dblogin();
DBSETLUSER(loginU, LoginID);
DBSETLPWD(loginU, Password);
DBSETLAPP(loginU, "UpStats_U");
dbprocU = dbopen(loginU, Server);
printf("connection complete.\n");
printf("Retrieving user table names from %s..sysobjects.\n", Database);
dbfcmd(dbprocS, "select name from %s..sysobjects", Database);
dbfcmd(dbprocS, " where type = 'u'");
printf("Updating statistics on the following tables: \n\n");
dbsqlexec(dbprocS);
if (dbresults(dbprocS) == SUCCEED)
{
Count = 0;
dbbind(dbprocS, 1, NTBSTRINGBIND, 0, Name);
while (dbnextrow(dbprocS) != NO_MORE_ROWS)
{
printf("%d: %s..%s ",++Count, Database, Name);
dbfcmd(dbprocU, " update statistics %s..%s ", Database, Name);
dbsqlexec(dbprocU);
if (dbresults(dbprocU) == SUCCEED)
printf("(complete)\n");
else
printf("(failed)\n");
};
};
dbclose(dbprocS);
dbclose(dbprocU);
printf("\nUpdate complete.\n");
}
int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS *dbproc;
int severity;
int dberr;
int oserr;
char *dberrstr;
char *oserrstr;
{
printf("\nDB-LIBRARY ERROR: %d\n", dberr);
printf("(%s)", dberrstr);
if (oserr != DBNOERR)
{
printf("\nOPERATING-SYSTEM ERROR: %d\n", oserr);
printf("(%s)",oserrstr);
};
return(INT_EXIT);
}
int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
DBPROCESS *dbproc;
DBINT msgno;
int msgstate;
int severity;
char *msgtext;
{
if (severity > 0)
{
printf("\nSQL SERVER MESSAGE: #%ld, state %d, severity %d\n",
msgno, msgstate, severity);
printf("(%s)",msgtext);
return(INT_EXIT);
}
}